/**
 * 
 */
package actions.accounts;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import models.accounts.GBILogin;
import models.accounts.GBRegister;
import models.accounts.RegisterResult;
import models.accounts.entity.GBUserInfo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import commonservice.jcaptcha.CaptchaService;

/**
 * @author asua
 *
 */
public class GBRegisterAction extends ActionSupport implements ModelDriven<GBUserInfo> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Object> resultMap=new HashMap<String, Object>();;
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
	private String sigImage;
	public String getSigImage() {
		return sigImage;
	}
	public void setSigImage(String sigImage) {
		this.sigImage = sigImage;
	}
	private GBUserInfo userInfo=new GBUserInfo();
	public GBUserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(GBUserInfo userInfo) {
		this.userInfo = userInfo;
	}
	private static Logger logger = Logger.getLogger(GBRegisterAction.class
			.getName());
	public String execute() throws SQLException{
		GBILogin login=(GBILogin) new ClassPathXmlApplicationContext("spring.xml").getBean("login_tool");
		login.init();
		if(userInfo.getUserName()!=null){
			String sid=ServletActionContext.getRequest().getSession().getId();
			if(!CaptchaService.getInstance().validate(sid, sigImage)){
				resultMap.put("error", RegisterResult.SIGIMAGEERROR);
				return ERROR;
			}
			GBRegister register=(GBRegister) new ClassPathXmlApplicationContext("spring.xml").getBean("register_tool");
			RegisterResult result=register.register(userInfo);
			if(result==RegisterResult.REGISTERSUCCESS){
				login.Login(userInfo.getUserName(), userInfo.getPassword(),true); 
				return SUCCESS;
			}else{
				resultMap.put("error", result);
				return ERROR;
			}
		}
		return INPUT;
	}
	public GBUserInfo getModel() {
		return userInfo;
	}
}
